<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="删除标志" prop="delFlag">
        <el-select v-model="queryParams.delFlag" placeholder="请选择删除标志" clearable>
          <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="热度" prop="heatDeg">
        <el-input v-model="queryParams.heatDeg" placeholder="请输入热度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="测评的数量" prop="evaluationCount">
        <el-input v-model="queryParams.evaluationCount" placeholder="请输入测评的数量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="粉丝数" prop="fansCount">
        <el-input v-model="queryParams.fansCount" placeholder="请输入粉丝数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd()"
          v-hasPermi="['safe:zone:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :fit="true" v-if="refreshTable" :row-class-name="tableRowClassName" v-loading="loading" :data="zoneList"
      row-key="zoneId" :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column label="专区id" align="center" prop="zoneId" />

      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="父专区id" align="center" prop="parentId" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="热度" align="center" prop="heatDeg" />
      <el-table-column label="测评的数量" align="center" prop="evaluationCount" />
      <el-table-column label="主页背景图" align="center" prop="homeBackground" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.homeBackground" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="头像地址" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="粉丝数" align="center" prop="fansCount" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="delFlag" width="180">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.delFlag" @change="changeDelFlag(scope.row)" active-color="#13ce66"
            inactive-color="#ff4949" active-text="启用" inactive-text="禁用" active-value="0" inactive-value="2">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['safe:zone:edit']">修改</el-button>
          <el-button size="mini" type="text" v-if="scope.row.parentId == 0" icon="el-icon-plus"
            @click="handleAdd(scope.row)" v-hasPermi="['safe:zone:add']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['safe:zone:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改专区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="主页背景图" prop="homeBackground">
          <image-upload :limit="1" v-model="form.homeBackground" />
        </el-form-item>
        <el-form-item label="头像地址" prop="avatar">
          <image-upload :limit="1" v-model="form.avatar" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZone, getZone, delZone, addZone, updateZone } from "@/api/safe/zone";
import TreeSelect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Zone",
  dicts: ['sys_common_status'],
  components: {
    TreeSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 专区表格数据
      zoneList: [],
      // 专区树选项
      zoneOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        delFlag: null,
        title: null,
        parentId: null,
        heatDeg: null,
        evaluationCount: null,
        fansCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeDelFlag(value) {

      let form = {
        zoneId: value.zoneId,
        delFlag: value.delFlag
      }
      updateZone(form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });

    },
    tableRowClassName({ row, rowIndex }) {
      if (row.parentId != 0) {
        return 'warning-row';
      } else {
        return 'success-row';
      }
    },
    /** 查询专区列表 */
    getList() {
      this.loading = true;
      listZone(this.queryParams).then(response => {
        this.zoneList = this.handleTree(response.data, "zoneId", "parentId");
        this.loading = false;
      });
    },
    /** 转换专区数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.zoneId,
        label: node.title,
        children: node.children
      };
    },
    /** 查询专区下拉树结构 */
    parentId() {
      listZone().then(response => {
        this.zoneOptions = [];
        const data = { zoneId: 0, title: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "zoneId", "parentId");
        this.zoneOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zoneId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        title: null,
        parentId: null,
        heatDeg: null,
        evaluationCount: null,
        homeBackground: null,
        avatar: null,
        fansCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.parentId();
      if (row != null && row.zoneId) {
        this.form.parentId = row.zoneId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加专区";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.parentId();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getZone(row.zoneId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专区";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.zoneId != null) {
            updateZone(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZone(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除专区编号为"' + row.zoneId + '"的数据项？').then(function () {
        return delZone(row.zoneId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .warning-row {
  background: rgb(244, 230, 212);

}

::v-deep .success-row {
  background: rgb(234, 241, 223);
}
</style>
